package com.mhy.shapeview.drawable

import android.annotation.SuppressLint
import android.graphics.Canvas
import android.graphics.Outline
import android.graphics.Path
import android.os.SystemClock
import android.view.GestureDetector
import android.view.MotionEvent
import android.view.View
import android.view.ViewOutlineProvider
import android.widget.TextView
import androidx.annotation.DrawableRes
import androidx.core.content.ContextCompat
import androidx.core.view.GestureDetectorCompat


/**
 * Created By Mahongyin
 * Date    2022/11/6 14:28
 *
 */
fun TextView.leftDrawable(size: Int, @DrawableRes drawableRes: Int) {
    val drawable = ContextCompat.getDrawable(context, drawableRes)
    drawable?.setBounds(0, 0, Utils.dp2px(context, size), Utils.dp2px(context, size))
    setCompoundDrawables(drawable, null, null, null)
}

fun TextView.topDrawable(size: Int, @DrawableRes drawableRes: Int) {
    val drawable = ContextCompat.getDrawable(context, drawableRes)
    drawable?.setBounds(0, 0, Utils.dp2px(context, size), Utils.dp2px(context, size))
    setCompoundDrawables(null, drawable, null, null)
}

fun TextView.rightDrawable(size: Int, @DrawableRes drawableRes: Int) {
    val drawable = ContextCompat.getDrawable(context, drawableRes)
    drawable?.setBounds(0, 0, Utils.dp2px(context, size), Utils.dp2px(context, size))
    setCompoundDrawables(null, null, drawable, null)
}

fun TextView.bottomDrawable(size: Int, @DrawableRes drawableRes: Int) {
    val drawable = ContextCompat.getDrawable(context, drawableRes)
    drawable?.setBounds(0, 0, Utils.dp2px(context, size), Utils.dp2px(context, size))
    setCompoundDrawables(null, null, null, drawable)
}

/**
 * 椭圆
 */
fun View.setOval() {
    setClipToOutline(true)
//    outlineSpotShadowColor = 轮廓颜色
//    outlineAmbientShadowColor = 轮廓颜色
    outlineProvider = object : ViewOutlineProvider() {
        override fun getOutline(view: View, outline: Outline) {
            outline.setOval(0, 0, view.width, view.height)
        }
    }
}

/**
 * 设置圆角
 */
fun View.setRoundRadius(dp: Float) {
    setClipToOutline(true)
//    outlineSpotShadowColor = 轮廓颜色
//    outlineAmbientShadowColor = 轮廓颜色
    outlineProvider = object : ViewOutlineProvider() {
        override fun getOutline(view: View, outline: Outline) {
            outline.setRoundRect(
                0, 0,
                view.width, view.height,
                Utils.dpToPx(context, dp)
            )
        }
    }
}


/**
 * View双击事件
 */
@SuppressLint("ClickableViewAccessibility")
inline fun <T : View> T.doubleClick(crossinline doubleClick: (T) -> Unit) {
    val gestureDetector = object : GestureDetector.SimpleOnGestureListener() {
        override fun onDoubleTap(e: MotionEvent): Boolean {
            doubleClick(this@doubleClick)
            return true
        }

        override fun onSingleTapUp(e: MotionEvent): Boolean {
            // 手指松开，ACTION_UP触发，单击行为  需要view设置了setOnClickListener
            this@doubleClick.performClick()
            return true
        }
    }
    val gestureDetectorCompat = GestureDetectorCompat(context, gestureDetector)
    this.setOnTouchListener { _, event ->
        gestureDetectorCompat.onTouchEvent(event)
        true
    }
}

private var lastClickTime: Long = 0
//点击防防抖
fun View.setOnClickListener(timeout: Long, listener: (View) -> Unit) {
    setOnClickListener {
        val currentTime = SystemClock.elapsedRealtime()
        if (currentTime - lastClickTime > timeout) {
            lastClickTime = currentTime
            listener(it)
        }
    }
}

private var mCountArray: LongArray? = null

/**
 * timeout ms内点击 count次 才执行
 */
fun View.setOnClickListener(count: Int, timeout: Long, listener: (View) -> Unit) {
    setOnClickListener {
        if (mCountArray == null) {
            mCountArray = LongArray(count)
        }
        mCountArray?.let {
            //把从第二位至最后一位之间的数字复制到第一位至倒数第一位
            System.arraycopy(it, 1, it, 0, it.size - 1)
            //记录一个时间
            it[it.size - 1] = SystemClock.uptimeMillis()
            //x毫秒内连续点击。
            if (SystemClock.uptimeMillis() - it[0] <= timeout) {
                //还原状态
                mCountArray = null
                //执行操作 输入密码
                listener(this)
            }
        }
    }
}